/* 2011/11/29 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Drawing;

namespace PictureManager
{
    /// <summary>
    /// Provides methods to serialize settings to a XML file and deserialize settings from a XML file.
    /// </summary>
    static class XmlSettingsSerializer
    {
        public static void Save(string settingsFilePath)
        {
            XmlWriterSettings writerSettings = new XmlWriterSettings();
            writerSettings.ConformanceLevel = ConformanceLevel.Fragment;
            writerSettings.Indent = true;
            writerSettings.NewLineOnAttributes = false;
            using (XmlWriter theWriter = XmlWriter.Create(settingsFilePath, writerSettings))
            {
                Properties.Settings settings = Properties.Settings.Default;
                theWriter.WriteStartElement("settings");

                theWriter.WriteStartElement("ImagePath");
                theWriter.WriteElementString("value", settings.ImagePath);
                theWriter.WriteEndElement();

                theWriter.WriteStartElement("SourcePath");
                theWriter.WriteElementString("value", settings.SourcePath);
                theWriter.WriteEndElement();

                theWriter.WriteStartElement("DestinationPath");
                theWriter.WriteElementString("value", settings.DestinationPath);
                theWriter.WriteEndElement();

                theWriter.WriteStartElement("FolderPaths");
                theWriter.WriteAttributeString("Count", settings.FolderPaths.Count.ToString());
                theWriter.WriteString(String.Empty);
                foreach (string aElment in settings.FolderPaths)
                    theWriter.WriteElementString("value", aElment);
                theWriter.WriteEndElement();

                theWriter.WriteStartElement("FolderPath");
                theWriter.WriteElementString("value", settings.FolderPath);
                theWriter.WriteEndElement();

                theWriter.WriteStartElement("FileNameAttributes");
                theWriter.WriteAttributeString("Count", settings.FileNameAttributes.Count.ToString());
                theWriter.WriteString(String.Empty);
                foreach (string aElment in settings.FileNameAttributes)
                    theWriter.WriteElementString("value", aElment);
                theWriter.WriteEndElement();

                theWriter.WriteStartElement("FileNameAttributeIndex");
                theWriter.WriteElementString("value", settings.FileNameAttributeIndex.ToString());
                theWriter.WriteEndElement();

                theWriter.WriteStartElement("SearcherName");
                theWriter.WriteElementString("value", settings.SearchName);
                theWriter.WriteEndElement();

                theWriter.WriteStartElement("ThemeName");
                theWriter.WriteElementString("value", settings.ThemeName);
                theWriter.WriteEndElement();

                theWriter.WriteStartElement("PlaySpeed");
                theWriter.WriteElementString("value",settings.PlaySpeed.ToString());
                theWriter.WriteEndElement();

                theWriter.WriteStartElement("PlayDisorder");
                theWriter.WriteElementString("value", settings.PlayDisorder.ToString());
                theWriter.WriteEndElement();

                theWriter.WriteStartElement("KeyCombinationExecutants");
                theWriter.WriteAttributeString("Count", settings.KeyCombinationExetants.Count.ToString());
                theWriter.WriteString(String.Empty);
                foreach (string aElement in settings.KeyCombinationExetants)
                    theWriter.WriteElementString("value", aElement);
                theWriter.WriteEndElement();

                theWriter.WriteStartElement("MouseGestureColor");
                theWriter.WriteElementString("value", settings.MouseGestureColor.ToArgb().ToString());
                theWriter.WriteEndElement();

                theWriter.WriteStartElement("MouseGestureWidth");
                theWriter.WriteElementString("value", settings.MouseGestureWidth.ToString());
                theWriter.WriteEndElement();

                theWriter.WriteStartElement("MouseGestureExecutants");
                theWriter.WriteAttributeString("Count", settings.MouseGestureExecutants.Count.ToString());
                theWriter.WriteString(String.Empty);
                foreach (string aElement in settings.MouseGestureExecutants)
                    theWriter.WriteElementString("value", aElement);
                theWriter.WriteEndElement();

                theWriter.WriteStartElement("RenameSelectionKeeping");
                theWriter.WriteElementString("value",settings.RenameSelectionKeeping.ToString());
                theWriter.WriteEndElement();

                theWriter.WriteStartElement("FileCorrespondenceRules");
                theWriter.WriteAttributeString("Count", settings.FileCorrespondenceRules.Count.ToString());
                theWriter.WriteString(String.Empty);
                foreach (string aElment in settings.FileCorrespondenceRules)
                    theWriter.WriteElementString("value", aElment);
                theWriter.WriteEndElement();

                theWriter.WriteStartElement("FileCorrespondedRootFolder");
                theWriter.WriteElementString("value",settings.FileCorrespondedRootFolder);
                theWriter.WriteEndElement();

                theWriter.WriteStartElement("HideBorderSelection");
                theWriter.WriteElementString("value", settings.HideBorderSelection.ToString());
                theWriter.WriteEndElement();

                /*
                theWriter.WriteStartElement("");
                theWriter.WriteElementString("value");
                theWriter.WriteEndElement();
                */

                theWriter.WriteEndElement();
                theWriter.Flush();
            }
        }

        public static void Load(string settingsFilePath)
        {
            XmlReaderSettings readerSettings = new XmlReaderSettings();
            readerSettings.ConformanceLevel = ConformanceLevel.Fragment;
            readerSettings.IgnoreWhitespace = true;
            readerSettings.IgnoreComments = true;
            using (XmlReader theReader = XmlReader.Create(settingsFilePath, readerSettings))
            {
                Properties.Settings settings = Properties.Settings.Default;
                int count = 0;
                theReader.ReadStartElement("settings");

                theReader.ReadStartElement("ImagePath");
                settings.ImagePath = theReader.ReadElementContentAsString();
                theReader.ReadEndElement();

                theReader.ReadStartElement("SourcePath");
                settings.SourcePath = theReader.ReadElementContentAsString();
                theReader.ReadEndElement();

                theReader.ReadStartElement("DestinationPath");
                settings.DestinationPath = theReader.ReadElementContentAsString();
                theReader.ReadEndElement();

                count = Int32.Parse(theReader.GetAttribute("Count"));
                theReader.ReadStartElement("FolderPaths");
                settings.FolderPaths.Clear();
                for (int i = 0; i < count; i++)
                    settings.FolderPaths.Add(theReader.ReadElementContentAsString());
                theReader.ReadEndElement();

                theReader.ReadStartElement("FolderPath");
                settings.FolderPath = theReader.ReadElementContentAsString();
                theReader.ReadEndElement();

                count = Int32.Parse(theReader.GetAttribute("Count"));
                theReader.ReadStartElement("FileNameAttributes");
                settings.FileNameAttributes.Clear();
                for (int i = 0; i < count; i++)
                    settings.FileNameAttributes.Add(theReader.ReadElementContentAsString());
                theReader.ReadEndElement();

                theReader.ReadStartElement("FileNameAttributeIndex");
                settings.FileNameAttributeIndex = theReader.ReadElementContentAsInt();
                theReader.ReadEndElement();

                theReader.ReadStartElement("SearcherName");
                settings.SearchName = theReader.ReadElementContentAsString();
                theReader.ReadEndElement();

                theReader.ReadStartElement("ThemeName");
                settings.ThemeName = theReader.ReadElementContentAsString();
                theReader.ReadEndElement();

                theReader.ReadStartElement("PlaySpeed");
                settings.PlaySpeed = theReader.ReadElementContentAsDecimal();
                theReader.ReadEndElement();

                theReader.ReadStartElement("PlayDisorder");
                settings.PlayDisorder = Boolean.Parse(theReader.ReadElementContentAsString());
                theReader.ReadEndElement();

                count = Int32.Parse(theReader.GetAttribute("Count"));
                theReader.ReadStartElement("KeyCombinationExecutants");
                settings.KeyCombinationExetants.Clear();
                for (int i = 0; i < count; i++)
                    settings.KeyCombinationExetants.Add(theReader.ReadElementContentAsString());
                theReader.ReadEndElement();

                theReader.ReadStartElement("MouseGestureColor");
                settings.MouseGestureColor = Color.FromArgb(theReader.ReadElementContentAsInt());
                theReader.ReadEndElement();

                theReader.ReadStartElement("MouseGestureWidth");
                settings.MouseGestureWidth = theReader.ReadElementContentAsFloat();
                theReader.ReadEndElement();

                count = Int32.Parse(theReader.GetAttribute("Count"));
                theReader.ReadStartElement("MouseGestureExecutants");
                settings.MouseGestureExecutants.Clear();
                for (int i = 0; i < count; i++)
                    settings.MouseGestureExecutants.Add(theReader.ReadElementContentAsString());
                theReader.ReadEndElement();

                    theReader.ReadStartElement("RenameSelectionKeeping");
                settings.RenameSelectionKeeping = theReader.ReadElementContentAsInt();
                theReader.ReadEndElement();

                count = Int32.Parse(theReader.GetAttribute("Count"));
                theReader.ReadStartElement("FileCorrespondenceRules");
                settings.FileCorrespondenceRules.Clear();
                for (int i = 0; i < count; i++)
                    settings.FileCorrespondenceRules.Add(theReader.ReadElementContentAsString());
                theReader.ReadEndElement();

                theReader.ReadStartElement("FileCorrespondedRootFolder");
                settings.FileCorrespondedRootFolder = theReader.ReadElementContentAsString();
                theReader.ReadEndElement();

                theReader.ReadStartElement("HideBorderSelection");
                settings.HideBorderSelection = theReader.ReadElementContentAsInt();
                theReader.ReadEndElement();

                /*
                theReader.ReadStartElement("");
                theReader.ReadElementContentAsString();
                theReader.ReadEndElement();
                */

                theReader.ReadEndElement();
            }
        }
    }
}
